<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>订单审核</title>
<meta name="decorator" content="default" />
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/demo/demo.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/font-awesome-4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="${ctxStatic}/easyui/jquery.min.js"></script>
<script type="text/javascript"
	src="${ctxStatic}/easyui/jquery.easyui.min.js"></script>
<style type="text/css">
	.fa{
		font-size: 18px;
	}

</style>
</head>
<body>
	<%-- <ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/erp/warehouse/storage">入库</a></li>
		<li><a href="${ctx}/erp/warehouse/outBound">出库</a></li>
	</ul> --%>
	<table id="dg" title="订单审核"
		style="width: 100%; height: auto" data-options="
				rownumbers:true,
				singleSelect:true,
				autoRowHeight:false,
				pagination:true,
				pageSize:10">
		<thead>
			<tr>
				<th data-options="field:'orderCode',width:200,align:'center'">出入库编号</th>
				<th data-options="field:'type',width:100,align:'center',formatter:function(value,row,index){
						if(value==true){
							return '<i class= &quot;fa fa-sign-in &quot; />入库';
						}
						if(value==false){
							return '<i class= &quot;fa fa-sign-out &quot; />出库';
						}
					}">类型</th>
				<th data-options="field:'status',width:100,align:'center',formatter:function(value,row,index){
						if(value=='1'){
							return ' <i class= &quot;fa fa-check &quot; style=&quot;color:#1D9D73 &quot; />已审核';
						}
						if(value=='0'){
							return ' <i class= &quot;fa fa-question &quot; />待审核';
						}
						if(value=='2'){
							return '<i class= &quot;fa fa-times &quot;style=&quot;color:#FF0000 &quot;  />拒绝';
						}
					}">状态</th>
				<th data-options="field:'executor',width:150,align:'center'">申请人</th>
				<th data-options="field:'time',width:200,align:'center'">申请时间</th>
				<th data-options="field:'content',width:50,align:'center'">详情</th>
				<th data-options="field:'edit',width:50,align:'center',formatter:function(value,row,index){
						if(value=='0'){
							return ' <i class= &quot;fa fa-pencil-square-o &quot; style=&quot;color:#1D9D73 &quot; />';
						}
					}">操作</th>
			</tr>
		</thead>
	</table>
	<!-- 订单详情 -->
	<div id="dlg" class="easyui-dialog" title="订单" style="width:60%;height:auto;padding:10px"
			data-options="
				iconCls: 'icon-save',
				buttons: [{
					text:'Ok',
					iconCls:'icon-ok',
					handler:function(){
						alert('ok');
					}
				},{
					text:'Cancel',
					handler:function(){
						alert('cancel');;
					}
				}]
			">
		<div style="margin-bottom: 5px">
			<span>编号：</span><input  class='easyui-textbox'  style='width:20%;height:22px' readonly="true" />
			<span >申请日期：</span> <input class='easyui-textbox'  style='width:40%;height:22px' readonly="true" >
		</div>
		<div style="margin-bottom: 5px">
			<span >执行人：</span> <input class='easyui-textbox'  style='width:20%;height:22px' readonly="true" >
			<span >操作人：</span> <input class='easyui-textbox'  style='width:20%;height:22px' readonly="true" >
		</div>
		<table id="gstb" title="物品详情"
			style="width: 100%; height: auto" data-options="
					rownumbers:true,
					singleSelect:true,
					autoRowHeight:false,
					pagination:true,
					pageSize:10">
		<thead>
			<tr>
				<th data-options="field:'gscode',width:100,align:'center'">物品编号</th>
				<th data-options="field:'gsname',width:180,align:'center'">物品名称</th>
				<th data-options="field:'gsamount',width:80,align:'center'">物品数量</th>
				<th data-options="field:'remarks',width:150,align:'center'">备注</th>
			</tr>
		</thead>
	</table>

		<br/>
</div>
	<div id="tb" style="height:auto">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">追加</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">移除</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>
	</div>

<!-- 订单修改 -->
<div id="editdlg" class="easyui-dialog" title="订单" style="width:60%;height:auto;padding:10px">
		<div style="margin-bottom: 5px">
			<span>编号：</span><input  class='easyui-textbox'  style='width:20%;height:22px' readonly="true" id="edOrdersCode" />
			<span >申请日期：</span> <input class='easyui-textbox'  style='width:40%;height:22px' readonly="true" id="edApplyDate" />
		</div>
		<div style="margin-bottom: 5px">
			<span >执行人：</span> <input class='easyui-textbox'  style='width:20%;height:22px' readonly="true" id="edExecutor" />
			<span >操作人：</span> <input class='easyui-textbox'  style='width:20%;height:22px' readonly="true" id="edOperator" />
		</div>
		<table id="edgstb" class="easyui-datagrid" title="记录清单" style="height:auto"
			data-options="
				iconCls: 'icon-edit',
				singleSelect: true,
				toolbar: '#tb',
				rownumbers:true,
				onClickCell: onClickCell
			">
		<thead>
			<tr>
				<th data-options="field:'gscode',width:100,editor:{  
								        type:'textbox',  
								        options:{  
								        	readonly:true,
								        	precision:0,
								            missingMessage:'数量必填!'  
								        }  
								    } ">物品编号</th>
				<th data-options="field:'gsname',width:500,editor:{  
								        type:'combogrid',  
								        options:{  
								        	precision:0,
								            required:true ,   
								            missingMessage:'物品必填!',
								            idField:'name',  // idField:'codes',  
		                                    textField:'name',     
		                                    multiple:false,//多选按钮  
		                                    pagination : false,//是否分页   
		                                    mode : 'remote',//远程连接方式  
		                                    fitColumns:true,//自动大小    
		                                    //queryParams:searchParam,//当请求远程数据时，发送的额外参数  
		                                    method:'get',//请求方式  
		                                    dataType:'json',  
		                                    panelWidth:800,
		                                    columns:[[     
												{field:'name',title:'名称',fitColumns:true},		
												{field:'code',title:'编码',fitColumns:true},   
												{field:'amount',title:'库存量',fitColumns:true},  
												{field:'type',title:'类型',fitColumns:true}  
		                                    ]], 
		                                    onSelect:function(recordidex,rowData){
		                                    	returnGCode(recordidex,rowData);
		                                    },
		                                    keyHandler:{ 
		                                    	 up: function(){},  
		                                         down: function(){},  
		                                         enter: function(){},
		                                         query:function(q){
		                                         	quickDimQuery(q);
		                                         }
		                                    }}}">物品名称</th>
				<th data-options="field:'gsamount',width:100,editor:{  
								        type:'numberbox',  
								        options:{  
								        	precision:0,
								            required:true ,   
								            missingMessage:'数量必填!'  
								        }  
								    }">物品数量</th>
				<th data-options="field:'remarks',width:200,align:'left',editor:{type:'textbox'}">备注</th>
			</tr>
		</thead>
	</table>

		<br/>
</div>


	<script>
		function pagerFilter(data) {
			if (typeof data.length == 'number'
					&& typeof data.splice == 'function') { // is array
				data = {
					total : data.length,
					rows : data
				}
			}
			var dg = $(this);
			var opts = dg.datagrid('options');
			var pager = dg.datagrid('getPager');
			pager.pagination({
				onSelectPage : function(pageNum, pageSize) {
					opts.pageNumber = pageNum;
					opts.pageSize = pageSize;
					pager.pagination('refresh', {
						pageNumber : pageNum,
						pageSize : pageSize
					});
					dg.datagrid('loadData', data);
				}
			});
			if (!data.originalRows) {
				data.originalRows = (data.rows);
			}
			var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
			var end = start + parseInt(opts.pageSize);
			data.rows = (data.originalRows.slice(start, end));
			return data;
		}
		
		
		function getData() {
			var initData = '${listOrders}';
			initData=eval("("+initData+")");
			var rows = [];
			for(var i=0;i<initData.length;i++){
				rows.push({
					orderCode :initData[i].code,
					executor : initData[i].executor,
					time : initData[i].time,
					status : initData[i].status,
					type : initData[i].type,
					remarks : initData[i].remarks,
					content : '详情',
					edit : initData[i].status
				});
			} 
			return rows;
		}
		function getData2(data) {
			var rowindex=data;
			var initData = '${listOrders}';
			initData=eval("("+initData+")");
			var rows = [];
			for(var i=0;i<initData[rowindex].goods.length;i++){
				rows.push({
					gscode : initData[rowindex].goods[i].code,
					gsname : initData[rowindex].goods[i].name,
					gsamount : initData[rowindex].goods[i].amount,
					remarks	: initData[rowindex].goods[i].remarks
				});
			} 
			return rows;
		}
		
		$(function() {
			$('#dlg').dialog('close');
			$('#editdlg').dialog('close');
			
			$('#dg').datagrid({
				data : getData()
			}).datagrid({loadFilter:pagerFilter}).datagrid({
				onClickCell: function(index,field,value){
					var initData = '${listOrders}';
					initData=eval("("+initData+")");
					if(field=='content'){
						$('#editdlg').dialog('close');
						var button =[];
						if(initData[index].type==false&&initData[index].status=='0'){
							button =[{
								text:'审核通过',
								iconCls:'icon-ok',
								handler:function(){
									
									$.messager.confirm('审核通过', '确认审核通过?', function(r){
										if (r){
											ajMarking(initData[index].code,'1'); 
										}
									});
								}
							},{
								text:'拒绝',
								iconCls:'icon-no',
								handler:function(){
									$.messager.confirm('审核拒绝', '否定订单?', function(r){
										if (r){
											ajMarking(initData[index].code,'2');
										}
									});
								}
							}];
						}else{
							button =[{
								text:'取消',
								handler:function(){
									$('#dlg').dialog('close');
								}
							}];
						}
						$("#dlg").dialog({
							onOpen:function(){
								$("#_easyui_textbox_input1").val(initData[index].code);
								$("#_easyui_textbox_input2").val(initData[index].approvalDate);
								$("#_easyui_textbox_input3").val(initData[index].executor);
								$("#_easyui_textbox_input4").val(initData[index].operator);
								//表格中的物品详情
								$('#gstb').datagrid({
									data : getData2(index)
								}).datagrid({loadFilter:pagerFilter});
							},
							buttons:button
						});
					}
					//编辑
					if(field=='edit'&&value=='0'){
						$('#dlg').dialog('close');
						$("#editdlg").dialog({
							onOpen:function(){
								$("#edOrdersCode").textbox('setValue',initData[index].code);
								$("#edApplyDate").textbox('setValue',initData[index].approvalDate);
								$("#edExecutor").textbox('setValue',initData[index].executor);
								$('#edOperator').textbox('setValue',initData[index].operator);
								//表格中的物品详情
								$('#edgstb').datagrid({
									data : getData2(index)
								}).datagrid({loadFilter:pagerFilter});
							},
							buttons:[{
								text:'订单修正',
								handler:function(){
									console.log(editIndex);
									if(undefined!=editIndex){
										$.messager.alert('提示','请先点击保存!','info');
										return;
									}
									var rows = $("#edgstb").datagrid("getRows");
									var nameList=[];
									var amountList=[];
									var remarksList=[];
									for(var i=0;i<rows.length;i++){
										nameList.push(rows[i].gscode);//编号
										amountList.push(rows[i].gsamount);//数量
										remarksList.push(rows[i].remarks);//备注
									}
									var code=$("#edOrdersCode").textbox('getValue');
									_list={'remarksList':remarksList.toString(),
											'nameList':nameList.toString(),
											'amountList':amountList.toString(),'code':code
											};
									$.ajax({
									    url: '${ctx}/erp/warehouse/updateOrdersInfo',         
									    data: _list,
									    dataType: "json",
									    type: "POST",          
									    success: function (data) {
									    	$.messager.alert('完成','订单修改完成!','info',function(){
								        		window.location.href="${ctx}/erp/warehouse/toCheckOrder";
								        	});
									    },
									    error: function(){
									    	$.messager.alert('出错','出现未知错误，会抓紧修正!','error');
									    }
									}); 
								}
							}]
						});
					}
				}
			});
		});
		
		//异步审核或否定
		function ajMarking(code,action){
			var data ={};
			data={'code':code,'action':action};
			$.ajax({
			    url: '${ctx}/erp/warehouse/ajMarking',         
			    data: data,
			    dataType: "json",
			    type: "POST",          
			    success: function (data) {
			    	if("1"==data){//操作完成
						$.messager.alert('信息','操作完成!','info',function(){
							window.location.href='${ctx}/erp/warehouse/toCheckOrder';
			    		});
			    	}else if("0"){
			    		//库存不足
			    		$.messager.alert('警告','当前仓库物件数量不足!','error',function(){
			    			$('#dlg').dialog('close');
			    		});
			    	}
			    }
			});
			
		}
		
		//编辑
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#edgstb').datagrid('validateRow', editIndex)){
				$('#edgstb').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field){
			if (editIndex != index){
				if (endEditing()){
					$('#edgstb').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					var ed = $('#edgstb').datagrid('getEditor', {index:index,field:field});
					if (ed){
						($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
					}
					editIndex = index;
				} else {
					setTimeout(function(){
						$('#edgstb').datagrid('selectRow', editIndex);
					},0);
				}
			}
		}
		
		function append(){
			if (endEditing()){
				$('#edgstb').datagrid('appendRow',{status:'P'});
				editIndex = $('#edgstb').datagrid('getRows').length-1;
				$('#edgstb').datagrid('selectRow', editIndex)
						.datagrid('beginEdit', editIndex);
			}
		}
		function removeit(){
			if (editIndex == undefined){return}
			$.messager.confirm('提示信息' , '确认删除?' , function(r){  
            	if(r){  
            		$('#edgstb').datagrid('cancelEdit', editIndex)
					.datagrid('deleteRow', editIndex);
					editIndex = undefined;
                } else {  
                     return ;  
                } 
            });
		}
		function accept(){
			if (endEditing()){
				$('#edgstb').datagrid('acceptChanges');
			}
		}
		//组合网格模糊快速查询
		function quickDimQuery(q){
			var url = 'dimQueryGoods';
        	var row = $('#edgstb').datagrid('getSelected');  
            var rowIndex = $('#edgstb').datagrid('getRowIndex',row);//获取行号  
            var thisTarget = $('#edgstb').datagrid('getEditor', {'index':rowIndex,'field':'gsname'}).target;
            var grid =thisTarget.combogrid('grid');
            if(q!=null&&""!=q){
            	$(grid).datagrid('options').url= url+"?key="+q;
            }else{
            	$(grid).datagrid('options').url= url;  
            }
            $(grid).datagrid('load');
            thisTarget.combogrid('setValue', q);
		}
		//选中物品栏自动填充物品编号
		function returnGCode(recordidex,rowData){
			var row = $('#edgstb').datagrid('getSelected');  
            var rowIndex = $('#edgstb').datagrid('getRowIndex',row);//获取行号  
            var thisTarget = $('#edgstb').datagrid('getEditor', {'index':rowIndex,'field':'gscode'});
            $(thisTarget.target).textbox('setValue', rowData.code);//设置值
		}
		
	</script>

</body>
</html>